package main

import (
	"container/list"
	"math"
)

type LRUCache struct {
	Len int
	l   *list.List
	m   map[int]*list.Element
}

type Pair struct {
	Key   int
	Value int
}

func Constructor(l int) LRUCache {
	return LRUCache{
		Len: l,
		l:   list.New(),
		m:   make(map[int]*list.Element),
	}
}

func (c *LRUCache) Set(key int, value int) {
	if len(c.m) == c.Len {
		deleteNode := c.l.Back()
		key := deleteNode.Value.(Pair).Key
		delete(c.m, key)
		c.l.Remove(deleteNode)
	}

	v := Pair{Key: key, Value: value}
	newNode := c.l.PushFront(v)
	c.m[key] = newNode
}

func (c *LRUCache) Get(key int) int {
	if v, ok := c.m[key]; ok {
		node := v
		c.l.MoveToFront(node)
		return node.Value.(Pair).Value
	}

	return math.MinInt
}
